﻿using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Security.Cryptography;

namespace Realize
{
    public class CFFK : ICFFK
    {
        public async Task<int> AddCFFKAsync(Config_File_First_Kind cf,string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"insert into [dbo].[config_file_first_kind]([first_kind_id], [first_kind_name], [first_kind_salary_id], [first_kind_sale_id])
                 values('{cf.first_kind_id}','{cf.first_kind_name}','{cf.first_kind_salary_id}','{cf.first_kind_sale_id}') ";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<int> DeleteCFFKAsync(int fkk_id,string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {

                string sql = $@"delete from [dbo].[config_file_first_kind] where [ffk_id]='{fkk_id}'";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<List<Config_File_First_Kind>> SelectCFFKAsync(string sqlserver)
        {
            Task<List<Config_File_First_Kind>> task = Task<List<Config_File_First_Kind>>.Run(() =>
            {
                using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
                {

                    string sql = $@"select * from [dbo].[config_file_first_kind]";

                    return sqlConnection.Query<Config_File_First_Kind>(sql).ToList();
                }

            });

            return await task;
        }

        public async Task<Config_File_First_Kind?> SelectCFFKByIDAsync(string sqlserver, string firstKindId)
        {
            string sql = "SELECT * FROM config_file_first_kind WHERE first_kind_id = @FirstKindId";
            using (var connection = new SqlConnection(sqlserver))
            {
                await connection.OpenAsync();
                return await connection.QueryFirstOrDefaultAsync<Config_File_First_Kind>(
                    sql, new { FirstKindId = firstKindId }
                );
            }
        }

        public async Task<Config_File_First_Kind?> SelectCFFKIDAsync(int fkk_id, string sqlserver)
        {
            Task<Config_File_First_Kind?> task = Task<Config_File_First_Kind>.Run(() =>
            {
                using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
                {

                    string sql = $@"select * from [dbo].[config_file_first_kind] where [ffk_id]='{fkk_id}'";

                    return sqlConnection.Query<Config_File_First_Kind>(sql).FirstOrDefault();
                }

            });

            return await task;
        }

        public async Task<int> UpdateCFFKAsync(Config_File_First_Kind cd, string sqlserver,int id)
        {

            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {

                string sql = $@"update [dbo].[config_file_first_kind] set [first_kind_id]='{cd.first_kind_id}', [first_kind_name]='{cd.first_kind_name}', [first_kind_salary_id]='{cd.first_kind_salary_id}', [first_kind_sale_id]='{cd.first_kind_sale_id}' where [ffk_id]='{id}'";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

    }
}
